﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicTacToe
{
    class Program
    {
        static void Main(string[] args)
        {
            TicTacToe.Display();
            Console.WriteLine();
            Console.WriteLine(TicTacToe.CheckForWin() ? "WIN" : "NO WIN");
            Console.ReadKey();
        }
    }

    class TicTacToe
    {
        static TicTacToe()
        {
            matrix[0, 0] = Piece.Zero;
            matrix[0, 1] = Piece.Cross;
            matrix[0, 2] = Piece.Cross;
            matrix[0, 3] = Piece.Cross;

            matrix[1, 0] = Piece.Empty;
            matrix[1, 1] = Piece.Cross;
            matrix[1, 2] = Piece.Zero;
            matrix[1, 3] = Piece.Cross;

            matrix[2, 0] = Piece.Cross;
            matrix[2, 1] = Piece.Zero;
            matrix[2, 2] = Piece.Empty;
            matrix[2, 3] = Piece.Cross;

            matrix[3, 0] = Piece.Cross;
            matrix[3, 1] = Piece.Zero;
            matrix[3, 2] = Piece.Cross;
            matrix[3, 3] = Piece.Zero;
        }

        static Piece[,] matrix = new Piece[4,4];
        public static bool CheckForWin()
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                if (CheckHoriztal(i, Piece.Cross))
                    return true;
                if (CheckVertical(i, Piece.Cross))
                    return true;
                if (CheckForwardDiagonal(i, Piece.Cross))
                    return true;
                if (CheckBackwardDiagonal(i, Piece.Cross))
                    return true;

                if (CheckHoriztal(i, Piece.Zero))
                    return true;
                if (CheckVertical(i, Piece.Zero))
                    return true;
                if (CheckForwardDiagonal(i, Piece.Zero))
                    return true;
                if (CheckBackwardDiagonal(i, Piece.Zero))
                    return true;
            }
            return false;
        }

        private static bool CheckBackwardDiagonal(int i, Piece piece)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (matrix[j, matrix.GetLength(0) - j - 1] != piece)
                    return false;
            }
            return true;
        }

        private static bool CheckForwardDiagonal(int i, Piece piece)
        {            
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (matrix[j, j] != piece)
                    return false;                
            }
            return true;
        }

        private static bool CheckVertical(int i, Piece piece)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (matrix[j, i] != piece)
                    return false;
            }
            return true;
        }

        private static bool CheckHoriztal(int i, Piece piece)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (matrix[i, j] != piece)
                    return false;
            }
            return true;
        }

        public static void Display()
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(0); j++)
                {
                    if (matrix[i, j] == Piece.Empty)
                        Console.Write("_  ");
                    else if (matrix[i, j] == Piece.Cross)
                        Console.Write("x  ");
                    else
                        Console.Write("o  ");
                }
                Console.WriteLine();
            }   
        }
    }
    enum Piece
    {
        Empty, Cross, Zero
    }
}
